//
//  AppDelegate.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/7.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "AppDelegate.h"
#import "AppDelegate+ui.h"
#import "AppDelegate+NRWJPush.h"
#import "WXApi.h"
#import <JSPatchPlatform/JSPatch.h>
#import "NRWAppVersionManager.h"
#import "NRWAppUpdataVC.h"
#import <Bugly/Bugly.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	
	[JSPatch stwa43:@"81e222c96e01773c"];
	//    [JSPatch testscjsinbund];
	[JSPatch setupRSAPublicKey:@"-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3qCOFQGSDNGzZgt4a08hISTyq\ndjhI3XuDP8JQSZFcw3h3Em5P1TH+a/rXS1qKpIfH6xRRXeu+dVey4bQiydKUa1xw\nJ+kxxSZjoQZlMr+HUr6Ry40cmgQdtY6oqO+ggCbETCHPn7PFyp6BisjUZX0dXTuT\nVMyX8myqhD8+RGYT7QIDAQAB\n-----END PUBLIC KEY-----"];
	[JSPatch sync];
    
    [self setupWindow:launchOptions];
	[self creatBugly];
    [self initJPushWithWithOptions:launchOptions];
    [self wechat];
    
    [[NRWAppVersionManager sharedInstance]configureApp];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldUpdateApp:) name:kNotificationAppShouldUpdate object:nil];

    return YES;
}

-(void)creatBugly{
	BuglyConfig * config = [[BuglyConfig alloc] init];
	config.debugMode = YES;
	config.blockMonitorEnable = YES;
	config.blockMonitorTimeout = 1.5;
	config.channel = @"Bugly";
	config.consolelogEnable = NO;
	config.viewControllerTrackingEnable = NO;
	// Start the Bugly sdk with APP_ID and your config
	[Bugly startWithAppId:@"414202bf49"
#if DEBUG
		developmentDevice:YES
#endif
				   config:config];
	[Bugly setUserIdentifier:[NSString stringWithFormat:@"User: %@", [UIDevice currentDevice].name]];
	[Bugly setUserValue:[NSProcessInfo processInfo].processName forKey:@"Process"];
}

- (void)shouldUpdateApp:(NSNotification *)notification {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        id userInfo = notification.object;
        NRWAppUpdataVC *updateVC = [[NRWAppUpdataVC alloc] init];
        updateVC.urlStr = [userInfo objectForKey:@"URI"];
        [self.window setRootViewController:updateVC];
        [self.window makeKeyAndVisible];

    });
}
-(void)wechat{
    
    [WXApi registerApp:@"wxba829107dca702f6"];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [UIApplication sharedApplication].applicationIconBadgeNumber =0;
    [self setPushBage:0];

}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
